package com.more.spring.boot.springbootmybatisdemo.controller;


import com.more.spring.boot.springbootmybatisdemo.pojo.PageResult;
import com.more.spring.boot.springbootmybatisdemo.pojo.Result;
import com.more.spring.boot.springbootmybatisdemo.pojo.User;
import com.more.spring.boot.springbootmybatisdemo.service.UserService;
import io.micrometer.common.util.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/queryUserById")
    public User queryUserById(int id) {
        User user = userService.queryUserById(id);
        return user;
    }

    @RequestMapping("/queryUserList")
    public List<User> queryUserList() {
        List<User> users = userService.queryUserList();
        return users;
    }

    @RequestMapping("/queryUserByNameAndGender")
    public PageResult queryUserByNameAndGender(@RequestBody User user) {
        return userService.queryUserByNameAndGender(user);
    }


    @RequestMapping("/queryLimitUserList")
    public PageResult queryLimitUserList(@RequestParam(defaultValue = "1", name = "currentPage") int currentPage, @RequestParam(defaultValue = "10", name = "pageSize") int pageSize) {
        return userService.queryLimitUserList(currentPage, pageSize);
    }

    @RequestMapping("/addNewUser")
    public ResponseEntity<Result> addNewUser(@RequestBody User user) {
        try {
            int result = userService.addNewUser(user);
            return result > 0 ? ResponseEntity.ok(new Result(200, "ok", null)) : ResponseEntity.ok(new Result(500, "error", null));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Result(500, "服务器内部错误", null));
        }
    }

    @RequestMapping("/deleteUserById")
    public ResponseEntity<Result> deleteUserById(@Param("id") int id) {
        try {
            int result = userService.deleteUserById(id);
            return result > 0 ? ResponseEntity.ok(new Result(200, "ok", null)) : ResponseEntity.ok(new Result(500, "error", null));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Result(500, "服务器内部错误", null));
        }
    }

    @RequestMapping("/updateUser")
    public ResponseEntity<Result> updateUser(@RequestBody User user) {
        try {
            int result = userService.updateUser(user);
            return result > 0 ? ResponseEntity.ok(new Result(200, "ok", null)) : ResponseEntity.ok(new Result(500, "error", null));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Result(500, "服务器内部错误", null));
        }
    }

}
